package com.getpebble.android.core.sync;

import android.accounts.Account;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.main.sections.settings.SettingsUtil;
import java.lang.ref.WeakReference;

public class PblSyncManager {
    private static final String TAG = PblSyncManager.class.getSimpleName();
    private static PblSyncManager sInstance = null;
    private WeakReference<Context> mContext = null;

    private PblSyncManager(Context context) {
        this.mContext = new WeakReference(context);
    }

    public static PblSyncManager createInstance(Context context) throws IllegalArgumentException {
        Trace.debug(TAG, "createInstance start");
        if (context == null) {
            throw new IllegalArgumentException("'context' may not be null!");
        }
        if (sInstance == null) {
            sInstance = new PblSyncManager(context);
        }
        Trace.debug(TAG, "createInstance stop");
        return sInstance;
    }

    private void sync(int action) {
        Trace.info(TAG, "sync start; action: " + action);
        Account userAccount = PebbleApplication.getSessionManager().getUserAccount();
        if (userAccount != null) {
            Bundle settingsBundle = new Bundle();
            settingsBundle.putBoolean("force", true);
            settingsBundle.putBoolean("expedited", true);
            settingsBundle.putInt("sync_target", action);
            ContentResolver.requestSync(userAccount, "com.getpebble.android.internal.provider", settingsBundle);
        } else {
            Trace.error(PblSyncManager.class.getSimpleName(), "No user account available!");
        }
        Trace.info(TAG, "sync stop");
    }

    public void syncAll() {
        sync(0);
    }

    public void syncLockerApps() {
        sync(1);
    }

    public void setPeriodicSync() {
        Trace.info(TAG, "setPeriodicSync start");
        Account userAccount = PebbleApplication.getSessionManager().getUserAccount();
        if (userAccount == null) {
            Trace.warning(TAG, "setPeriodicSync failed, user account is null");
            return;
        }
        Bundle settingsBundle = new Bundle();
        settingsBundle.putInt("sync_target", 0);
        long syncTime = 86400000;
        Context context = (Context) this.mContext.get();
        if (context != null) {
            syncTime = SettingsUtil.getSyncIntervalMs(new PblPreferences(context));
        }
        ContentResolver.addPeriodicSync(userAccount, "com.getpebble.android.internal.provider", settingsBundle, syncTime / 1000);
        ContentResolver.setSyncAutomatically(userAccount, "com.getpebble.android.internal.provider", true);
        Trace.info(TAG, "setPeriodicSync stop");
    }
}
